<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="手机号" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="userPassword">
      <el-input v-model="dataForm.userPassword" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="userImg">
      <el-input v-model="dataForm.userImg" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="userAddrSid">
      <el-input v-model="dataForm.userAddrSid" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="userAddrCid">
      <el-input v-model="dataForm.userAddrCid" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="userAddrQid">
      <el-input v-model="dataForm.userAddrQid" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="userSex">
      <el-input v-model="dataForm.userSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="userBirthday">
      <el-input v-model="dataForm.userBirthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="userContent">
      <el-input v-model="dataForm.userContent" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userPhone: '',
          userName: '',
          userPassword: '',
          userImg: '',
          userAddrSid: '',
          userAddrCid: '',
          userAddrQid: '',
          userSex: '',
          userBirthday: '',
          userContent: ''
        },
        dataRule: {
          userPhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          userPassword: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          userImg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          userAddrSid: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          userAddrCid: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          userAddrQid: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          userSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          userBirthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          userContent: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userPhone = data.user.userPhone
                this.dataForm.userName = data.user.userName
                this.dataForm.userPassword = data.user.userPassword
                this.dataForm.userImg = data.user.userImg
                this.dataForm.userAddrSid = data.user.userAddrSid
                this.dataForm.userAddrCid = data.user.userAddrCid
                this.dataForm.userAddrQid = data.user.userAddrQid
                this.dataForm.userSex = data.user.userSex
                this.dataForm.userBirthday = data.user.userBirthday
                this.dataForm.userContent = data.user.userContent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userPhone': this.dataForm.userPhone,
                'userName': this.dataForm.userName,
                'userPassword': this.dataForm.userPassword,
                'userImg': this.dataForm.userImg,
                'userAddrSid': this.dataForm.userAddrSid,
                'userAddrCid': this.dataForm.userAddrCid,
                'userAddrQid': this.dataForm.userAddrQid,
                'userSex': this.dataForm.userSex,
                'userBirthday': this.dataForm.userBirthday,
                'userContent': this.dataForm.userContent
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
